new Vue({
    el: '#main',
    data: () => ({
        listdata: [],
        page: 1,
        page_size: 30,
        status: '1',
        allid: [],
        batch: [],
        sort: [false, false, false],
        sortsorj: true,
        sortkey: '',
        dayin:{
            langname: 'zh',
            format: '1',
            selectdata: [],
            sort: '2',
            separate: '0',
            iconSize: '2',
            fontSize: '2',
        },
        langs: [],
    }),
    methods: {
        formatUnit: formatUnit,
        getdata() {
            $('.loding').show()
            $('.containerindex input').prop('checked', false)
            this.batch = [];
            $.ajax({
                url: '/admin/Logistics/container',
                data: {
                    status: this.status,
                    page: this.page,
                    pageSize: this.page_size
                },
                type: 'post',
                success: data => {
                    this.listdata = [];
                    data = JSON.parse(data)
                    this.allid.push(data.ids);
                    this.langs = data.langs;
                    data.ready_date.forEach(element => {
                        var obj = {date: element, val: [], sum_ctn: 0, sum_value: 0}
                        data.data.forEach(item => {
                            if (item.ready_date == element) {
                                obj.val.push(item)
                                obj.sum_ctn++
                                obj.sum_value += item.sum_value
                            }
                        })
                        this.listdata.push(obj)
                    });
                    if (data.size)
                        this.setPage(parseInt(this.page), data.size)
                    $('.loding').hide()
                }
            })
        },
        setPage(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.page_size;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">' + langcon.page_home + '</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>' + langcon.previous_page + '</a></li>',
                next: '<li class="next"><a href="javascript:;">' + langcon.next_page + '<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">' + langcon.last_page + '</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all: '<li class="page"><a href="javascript:;">' + myPageCount + ' / ' + this.page + 'PG , ' + this.page_size + ' /PG</a></li>',
                onPageChange: (num, type) => {
                    if (type == "change") {
                        this.page = num
                        this.getdata()
                    }
                }
            });
        },
        statuschange(status) {
            this.status = status
            this.page = '1';
            this.getdata()
        },
        allsel(event) {
            if ($(event.target).prop('checked')) {
                $(event.target).parents('table').find('input').prop('checked', true)
                this.batch = this.allid
            } else {
                $(event.target).parents('table').find('input').prop('checked', false)
                this.batch = []
            }
        },
        onesel(did) {
            if (this.batch.indexOf(did) == -1) {
                this.batch.push(did)
            } else {
                this.batch.splice(this.batch.indexOf(did), 1)
            }
            var isall = true
            $('td input').each(function () {
                if (!$(this).prop('checked')) {
                    isall = false
                }
            })
            $('th input').prop('checked', isall)
        },
        banckchangestatus() {
            console.log(this.batch)
            if (this.batch.length) {
                $('#container_batModal').modal({remote: '/admin/logistics/containerchang?ids=' + this.batch.join(',')})
            } else {
                change_mes_type('#headeralert', 2)
                $('#headeralert').show()
                $('#headeralert p').html($('#msg').val())
            }
        },
        apifun(type, n, id, name) {
            $.ajax({
                url: '/admin/logistics/' + type,
                data: {
                    num: n,
                    id,
                    name
                },
                type: 'post',
                success: data => {
                    data = JSON.parse(data)
                    change_mes_type('#headeralert', data.status)
                    $('#headeralert').show()
                    $('#headeralert p').html(data.msg)
                    if (data.status == 1) {
                        this.getdata()
                    }

                }
            })
        },
        sortoneData(a, b) {
            if (!this.sortsorj) {
                return b[this.sortkey] - a[this.sortkey]
            } else {
                return a[this.sortkey] - b[this.sortkey]
            }
        },
        sortfun(index, key) {  //排序
            this.sortkey = key
            this.listdata.forEach(item => {
                item.val.sort(this.sortoneData)
            });
            this.sortsorj = this.sort[index]
            this.sort[index] = !this.sort[index]
        },
        //弹出页面
        dayindata(ev){
            conid=ev.target.dataset.container_id;
            this.dayin.container_id = conid;
        },
        dayindatares(){
            $.ajax({
                url: '/admin/logistics/preview',
                data: {
                    setddatas: this.dayin,
                    setdata:'',
                },
                type: 'post',
                success: data => {
                    location.href = '/admin/Logistics/preview?onetwothree';
                }
            })
        },
        //获取地址栏的参数
        getQueryString(name){
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null)return unescape(r[2]);
            return null;
        },
        //分页存储
        gotocontainer(){
            var storage=window.localStorage;
            storage.setItem('pagecontainer',this.page);//分页
            storage.setItem("statuscontainer", this.status);//状态
        }
    },
    mounted() {
        var storage=window.localStorage;
        statuscode=this.getQueryString('status')
        if($('#formMenu').text() != 1){
            //货柜抵达跳转、返回列表
            if(statuscode){
                this.status=statuscode;
            }
            if(storage.getItem('pagecontainer')){
                this.page= storage.getItem("pagecontainer");
                this.status=storage.getItem('statuscontainer');
            }

        }else {
            this.status=this.page= 1;
        }
        storage.removeItem("pagecontainer");
        storage.removeItem('statuscontainer');
        this.getdata()
        $('#container_editModal').on('hide.bs.modal', () => {
            if (localStorage.getItem("model")) {
                this.getdata()
                localStorage.removeItem("model");
            }
        })
        $('#container_addModal').on('hide.bs.modal', () => {
            if (localStorage.getItem("model")) {
                this.getdata()
                localStorage.removeItem("model");
            }
        })
        $('#container_batModal').on('hide.bs.modal', () => {
            if (localStorage.getItem("model")) {
                this.getdata()
                localStorage.removeItem("model");
            }
        })
    }
})
$("#container_editModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(document).unbind('keydown')
});
$("#container_batModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(document).unbind('keydown')
});
$("#print_settingModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(document).unbind('keydown')
});
$("#container_addModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(document).unbind('keydown')
});